﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Resources;
using System.Globalization;
using System.Threading;

namespace SQL_EG.uc
{

    public partial class ucFirstStep : UserControl
    {

        ResourceManager res_man;    // declare Resource manager to access to specific cultureinfo
        CultureInfo culture;     
        private EntityManager entityManager = null;
       

        public ucFirstStep()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(cbxDatabase.Text))
                {
                    return;
                }
                Parametres param = new Parametres
                {
                    serverName = cbxServerName.Text,
                    databaseName = cbxDatabase.Text,
                    userName = tbxUser.Text,
                    pwd = tbxPwd.Text
                };

                uc.ucSecondStep ucSecond = new ucSecondStep(param);
                this.Hide();
                this.Parent.Controls.Add(ucSecond);
                this.Parent.Width = ucSecond.Width + 10;
                this.Parent.Height = ucSecond.Width + 230;
            }
            catch (Exception)
            {
            }

        }

        private async void LoadInstance()
        {
            PboxLoading.Visible = true;
                var entityManager = new EntityManager();
                    var result = entityManager.GetDefaultInstance();

                    cbxServerName.DataSource = await result;
           
                    cbxServerName.DisplayMember = "Name";
                    cbxServerName.ValueMember = "Name";
                   
            
            PboxLoading.Visible = false;
            cbxServerName.Enabled = true;
           
        }

        private void ucFirstStep_Load(object sender, EventArgs e)
        {
            cbxSource.SelectedIndex = 0;
            LoadInstance();
            LoadLanguage();
        }

        private void LoadLanguage()
        {
            res_man = new ResourceManager("SQL_EG.Resource.Res", typeof(ucFirstStep).Assembly);

            if (Thread.CurrentThread.CurrentCulture.Name == "fr-FR")
            {

                culture = CultureInfo.CreateSpecificCulture("fr");

                LblTitle.Text = res_man.GetString("lblTitle", culture);
                lblSource.Text = res_man.GetString("lblSource", culture);
                lblSever.Text = res_man.GetString("lblSever", culture);
                gpbxAuthentification.Text = res_man.GetString("gpbxAuthentification", culture);
                lblUser.Text = res_man.GetString("lblUser", culture);
                lblPwd.Text = res_man.GetString("lblPwd", culture);
                gpbxDatabase.Text = res_man.GetString("gpbxDatabase", culture);
                lblSelectDb.Text = res_man.GetString("lblSelectDb", culture);
                btnTest.Text = res_man.GetString("btnTest", culture);
            }
            else
            {
                culture = CultureInfo.CreateSpecificCulture("en");

                LblTitle.Text = res_man.GetString("lblTitle", culture);
                LblTitle.Text = res_man.GetString("lblTitle", culture);
                lblSource.Text = res_man.GetString("lblSource", culture);
                lblSever.Text = res_man.GetString("lblSever", culture);
                gpbxAuthentification.Text = res_man.GetString("gpbxAuthentification", culture);
                lblUser.Text = res_man.GetString("lblUser", culture);
                lblPwd.Text = res_man.GetString("lblPwd", culture);
                gpbxDatabase.Text = res_man.GetString("gpbxDatabase", culture);
                lblSelectDb.Text = res_man.GetString("lblSelectDb", culture);
                btnTest.Text = res_man.GetString("btnTest", culture);
            }

         


        }
      
        private void btnTest_Click(object sender, EventArgs e)
        {
            try
            {
                if (TestConnect())
                {
                    Tools.Notification.ThrowMessage(res_man.GetString("TestSuccess", culture));
                }
                else
                {
                    Tools.Notification.ThrowMessage(res_man.GetString("TestFailed", culture));
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void cbxDatabase_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                if (TestConnect())
                {
                    btnOK.Enabled = true;
                }            
            }
            catch (Exception)
            {

            }

        }

        private bool TestConnect()
        {
            bool result = false;
                entityManager = new EntityManager(cbxServerName.Text, tbxUser.Text, tbxPwd.Text);
                if (entityManager.ConnectoServer())
                {
                    result = true;
                }
            return result;
        }
    

    private void tbxPwd_Leave(object sender, EventArgs e)
        {
            cbxDatabase.Items.Clear();
        cbxDatabase.Text = string.Empty;
                try
                {
                    if (TestConnect())
                    {
                        
                        foreach (var item in entityManager.GetDatabaseList())
                        {
                            cbxDatabase.Items.Add(item);
                        }
                    }
                }
                catch (Exception)
                {

                }

        }
    }
}
